package cn.hesion.nio;
/**
 * ClassName: NIOFileChannel02 <br/>
 * Description: <br/>
 * date: 2021/1/21 17:19<br/>
 *
 * @author Hesion<br />
 * @version
 * @since JDK 1.8
 */

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @program: NettyLearn
 * @description:
 * @author: hesion
 * @create: 2021-01-21 17:19
 **/
public class NIOFileChannel02 {
    public static void main(String[] args) throws Exception{
        FileInputStream fileInputStream = new FileInputStream("1.txt");
        FileChannel channel01 = fileInputStream.getChannel();
        FileOutputStream fileOutputStream= new FileOutputStream("2.txt");
        FileChannel channel02 = fileOutputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(512);
        while (true){
            // 不知道文件的大小 所以循环读取
            int read = channel01.read(byteBuffer);
            if(-1 == read){
                break;
            }
            //进行状态反转
            byteBuffer.flip();
            channel02.read(byteBuffer);
        }
        fileInputStream.close();
        fileOutputStream.close();
    }
}
